package com.primeton.eos.oss.model.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.eos.spring.dao.CriteriaBuilder;
import com.primeton.eos.oss.model.dao.CrecAccountDao;
import com.primeton.eos.oss.model.dao.CrecAccountHisDao;
import com.primeton.eos.oss.model.dao.CrecSysOrganizationDao;
import com.primeton.eos.oss.model.dao.CrecSysOrganizationHisDao;
import com.primeton.eos.oss.model.dto.SynDataObjcetDto;
import com.primeton.eos.oss.model.dto.SynOrgDto;
import com.primeton.eos.oss.model.dto.SynUserDto;
import com.primeton.eos.oss.model.newdataset.CrecAccount;
import com.primeton.eos.oss.model.newdataset.CrecAccountHis;
import com.primeton.eos.oss.model.newdataset.CrecSysOrganization;
import com.primeton.eos.oss.model.newdataset.CrecSysOrganizationHis;
import com.primeton.eos.oss.model.newdataset.impl.CrecAccountHisImpl;
import com.primeton.eos.oss.model.newdataset.impl.CrecAccountImpl;
import com.primeton.eos.oss.model.newdataset.impl.CrecSysOrganizationHisImpl;
import com.primeton.eos.oss.model.newdataset.impl.CrecSysOrganizationImpl;
import com.primeton.eos.oss.model.utils.Sm4Utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;

@Service
public class MdmSynDealService {

	@Autowired
	CrecSysOrganizationDao crecSysOrganizationDao;

	@Autowired
	CrecSysOrganizationHisDao crecSysOrganizationHisDao;

	@Autowired
	CrecAccountDao mdmCrecAccountDao;

	@Autowired
	CrecAccountHisDao mdmCrecAccountHisDao;

	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;

	// 组织机构业务处理
	public void dealSynOrgs(String type, SynOrgDto org, SynDataObjcetDto obj) throws Exception  {
		if (null == obj) {
			return;
		}
		Date date = DateUtil.date();
		CrecSysOrganization crecSysOrganization = new CrecSysOrganizationImpl();
		crecSysOrganization.setMId(UuidUtils.generateUuid());
		crecSysOrganization.setMDataUid(obj.getId());
		crecSysOrganization.setMDataVersion(BigDecimal.ONE);
		crecSysOrganization.setMDataState(org.getIsDisabled() ? BigDecimal.ONE : BigDecimal.ZERO);
		crecSysOrganization.setMModelId(obj.getmDataModelId());
		crecSysOrganization.setMModelVersion(new BigDecimal(obj.getmVersionNum()));
		crecSysOrganization.setId(obj.getId());
		crecSysOrganization.setParentid(org.get_parent());
		crecSysOrganization.setOrganization(org.get_organization());
		crecSysOrganization.setCode(org.getCode());
		crecSysOrganization.setName(org.getName());
		crecSysOrganization.setFullname(org.getFullname());
		crecSysOrganization.setDescription(org.getDescription());
		crecSysOrganization.setSeq(
				StringUtils.isNotBlank(org.getSequence()) ? new BigDecimal(org.getSequence()) : BigDecimal.ZERO);
		crecSysOrganization.setIsdisabled(org.getIsDisabled() ? "1" : "0");
		crecSysOrganization.setCreateat(org.getCreateAt());
		crecSysOrganization.setOrgform(org.getOrgForm());
		crecSysOrganization.setEmail(org.getEmail());
		crecSysOrganization.setLinkaddress(org.getLinkAddress());
		crecSysOrganization.setSystemorgid(org.getSystemOrgId());
		crecSysOrganization.setDataType(org.getType());
		crecSysOrganization.setMDataFrom("OSS");
		crecSysOrganization.setMCreatedBy("admin");
		CrecSysOrganizationHis crecSysOrganizationHis = copyData(crecSysOrganization);
		CriteriaBuilder cb = new CriteriaBuilder(CrecSysOrganization.QNAME);
		cb.and("id", obj.getId());
		List<CrecSysOrganization> dbs = crecSysOrganizationDao.queryEntitiesByCriteria(cb.build());
		if (CollectionUtil.isEmpty(dbs)) {
			crecSysOrganization.setMCreatedAt(date);
			crecSysOrganizationDao.insertEntity(crecSysOrganization);
			crecSysOrganizationHis.setMCreatedAt(date);
			crecSysOrganizationHisDao.insertEntity(crecSysOrganizationHis);
		} else {
			crecSysOrganization.setMId(dbs.get(0).getMId());
			crecSysOrganization.setMDataVersion(dbs.get(0).getMDataVersion().add(BigDecimal.ONE));
			crecSysOrganizationHis.setMDataVersion(crecSysOrganization.getMDataVersion());
			crecSysOrganization.setUpdateat(org.getUpdateAt());
			crecSysOrganization.setMLastModifiedBy("admin");
			crecSysOrganization.setMLastModifiedAt(date);
			crecSysOrganizationHis.setMLastModifiedBy("admin");
			crecSysOrganizationHis.setMLastModifiedAt(date);
			if("DELETED".equals(type) || "DISABLED".equals(type)) {
				crecSysOrganization.setMDataState(BigDecimal.ONE);
				crecSysOrganizationHis.setMDataState(BigDecimal.ONE);
			}
			crecSysOrganizationDao.updateEntity(crecSysOrganization);
			crecSysOrganizationHisDao.updateEntity(crecSysOrganizationHis);
		}
	}

	public static void main(String[] args) {
		
	}
	private CrecSysOrganizationHis copyData(CrecSysOrganization crecSysOrganization) throws Exception  {
		CrecSysOrganizationHis crecSysOrganizationHis = new CrecSysOrganizationHisImpl();
		crecSysOrganizationHis.setMId(crecSysOrganization.getMId());
		crecSysOrganizationHis.setMDataUid(crecSysOrganization.getMDataUid());
		crecSysOrganizationHis.setMDataVersion(crecSysOrganization.getMDataVersion());
		crecSysOrganizationHis.setMDataState(crecSysOrganization.getMDataState());
		crecSysOrganizationHis.setMModelId(crecSysOrganization.getMModelId());
		crecSysOrganizationHis.setMModelVersion(crecSysOrganization.getMModelVersion());
		crecSysOrganizationHis.setId(crecSysOrganization.getId());
		crecSysOrganizationHis.setParentid(crecSysOrganization.getParentid());
		crecSysOrganizationHis.setOrganization(crecSysOrganization.getOrganization());
		crecSysOrganizationHis.setCode(crecSysOrganization.getCode());
		crecSysOrganizationHis.setName(crecSysOrganization.getName());
		crecSysOrganizationHis.setFullname(crecSysOrganization.getFullname());
		crecSysOrganizationHis.setDescription(crecSysOrganization.getDescription());
		crecSysOrganizationHis.setSeq(crecSysOrganization.getSeq());
		crecSysOrganizationHis.setIsdisabled(crecSysOrganization.getIsdisabled());
		crecSysOrganizationHis.setCreateat(crecSysOrganization.getCreateat());
		crecSysOrganizationHis.setOrgform(crecSysOrganization.getOrgform());
		crecSysOrganizationHis.setEmail(crecSysOrganization.getEmail());
		crecSysOrganizationHis.setLinkaddress(crecSysOrganization.getLinkaddress());
		crecSysOrganizationHis.setSystemorgid(crecSysOrganization.getSystemorgid());
		crecSysOrganizationHis.setDataType(crecSysOrganization.getDataType());
		crecSysOrganizationHis.setMDataFrom(crecSysOrganization.getMDataFrom());
		crecSysOrganizationHis.setMCreatedBy(crecSysOrganization.getMCreatedBy());
		return crecSysOrganizationHis;
	}

	private CrecAccountHis copyUserDatas(String uid, SynDataObjcetDto obj, SynUserDto user) throws Exception  {
		CrecAccountHis mdmCrecAccount = new CrecAccountHisImpl();
		mdmCrecAccount.setMId(uid);
		mdmCrecAccount.setMDataUid(obj.getId());
		mdmCrecAccount.setMDataVersion(new BigDecimal(obj.getmVersionNum()));
		mdmCrecAccount.setMDataState(user.isDisabled() ? BigDecimal.ONE : BigDecimal.ZERO);
		mdmCrecAccount.setMModelId(obj.getmDataModelId());
		mdmCrecAccount.setMModelVersion(new BigDecimal(obj.getmVersionNum()));
		mdmCrecAccount.setId(obj.getId());
		mdmCrecAccount.setUserid(obj.getId());
		mdmCrecAccount.setOrganization(user.get_organization());
		mdmCrecAccount.setUsername(user.getUsername());
		mdmCrecAccount.setPassword(user.getPassword());
		mdmCrecAccount.setFullname(user.getFullname());
		mdmCrecAccount.setIsdisabled(user.isDisabled() ? "1" : "0");
		mdmCrecAccount.setIslocked(user.getIsLocked() ? BigDecimal.ONE : BigDecimal.ZERO);
		mdmCrecAccount.setIssystem(user.getIsSystem() ? "1" : "0");
		mdmCrecAccount.setIspublic(user.getIsPublic() ? "1" : "0");
		mdmCrecAccount.setIsmaster(user.getIsMaster() ? "1" : "0");
		mdmCrecAccount.setIdnumber(user.getIDcard());
		mdmCrecAccount.setEmployeeno(user.getEmployeeNo());
		mdmCrecAccount.setEmail(user.getEmail());
		mdmCrecAccount.setSex(user.getSex());
		mdmCrecAccount.setMobile(user.getMobile()!=null&&!"".equals(user.getMobile())?Sm4Utils.decryptData(user.getMobile()):"");
		mdmCrecAccount.setSystemuserid(user.getSystemUserId());
		mdmCrecAccount.setMDataFrom("OSS");
		mdmCrecAccount.setMCreatedBy("admin");
		return mdmCrecAccount;
	}

	// 用户业务处理
	public void dealSynUser(String type, SynUserDto user, SynDataObjcetDto obj) throws Exception {
		if (null == obj) {
			return;
		}
		CrecAccount mdmCrecAccount = new CrecAccountImpl();
		String uid = UuidUtils.generateUuid();
		mdmCrecAccount.setMId(uid);
		mdmCrecAccount.setMDataUid(obj.getId());
		mdmCrecAccount.setMDataVersion(BigDecimal.ONE);
		mdmCrecAccount.setMDataState(user.isDisabled() ? BigDecimal.ONE : BigDecimal.ZERO);
		mdmCrecAccount.setMModelId(obj.getmDataModelId());
		mdmCrecAccount.setMModelVersion(new BigDecimal(obj.getmVersionNum()));
		mdmCrecAccount.setId(obj.getId());
		mdmCrecAccount.setUserid(obj.getId());
		mdmCrecAccount.setOrganization(user.get_organization());
		mdmCrecAccount.setUsername(user.getUsername());
		mdmCrecAccount.setPassword(user.getPassword());
		mdmCrecAccount.setFullname(user.getFullname());
		mdmCrecAccount.setIsdisabled(user.isDisabled() ? "1" : "0");
		mdmCrecAccount.setIslocked(user.getIsLocked() ? BigDecimal.ONE : BigDecimal.ZERO);
		mdmCrecAccount.setIssystem(user.getIsSystem() ? "1" : "0");
		mdmCrecAccount.setIspublic(user.getIsPublic() ? "1" : "0");
		mdmCrecAccount.setIsmaster(user.getIsMaster() ? "1" : "0");
		mdmCrecAccount.setIdnumber(user.getIDcard());
		mdmCrecAccount.setEmployeeno(user.getEmployeeNo());
		mdmCrecAccount.setEmail(user.getEmail());
		mdmCrecAccount.setSex(user.getSex());
		mdmCrecAccount.setMobile(user.getMobile()!=null&&!"".equals(user.getMobile())?Sm4Utils.decryptData(user.getMobile()):"");
		mdmCrecAccount.setSystemuserid(user.getSystemUserId());
		new CrecAccountHisImpl();
		mdmCrecAccount.setMDataFrom("OSS");
		mdmCrecAccount.setMCreatedBy("admin");
		CrecAccountHis mdmCrecAccountHis = copyUserDatas(uid, obj, user);
		Date date = DateUtil.date();
		CriteriaBuilder cb = new CriteriaBuilder(CrecAccount.QNAME);
		cb.and("id", obj.getId());
		List<CrecAccount> dbMdmCrecAccount = mdmCrecAccountDao.queryEntitiesByCriteria(cb.build());
		if (CollectionUtil.isEmpty(dbMdmCrecAccount)) {
			mdmCrecAccount.setMCreatedAt(date);
			mdmCrecAccountHis.setMCreatedAt(date);
			mdmCrecAccountDao.insertEntity(mdmCrecAccount);
			mdmCrecAccountHisDao.insertEntity(mdmCrecAccountHis);

		} else {
			CrecAccount db = dbMdmCrecAccount.get(0);
			mdmCrecAccount.setMId(db.getMId());
			mdmCrecAccount.setMDataVersion(db.getMDataVersion().add(BigDecimal.ONE));
			mdmCrecAccountHis.setMDataVersion(mdmCrecAccount.getMDataVersion());
			mdmCrecAccount.setUpdateat(user.getUpdateAt());
			mdmCrecAccount.setMLastModifiedBy("admin");
			mdmCrecAccount.setMLastModifiedAt(date);
			mdmCrecAccountHis.setUpdateat(user.getUpdateAt());
			mdmCrecAccountHis.setMLastModifiedBy("admin");
			mdmCrecAccountHis.setMLastModifiedAt(date);
			if("DELETED".equals(type) || "DISABLED".equals(type)) {
				mdmCrecAccount.setMDataState(BigDecimal.ONE);
				mdmCrecAccountHis.setMDataState(BigDecimal.ONE);
			}
			mdmCrecAccountDao.updateEntity(mdmCrecAccount);
			mdmCrecAccountHisDao.updateEntity(mdmCrecAccountHis);
		}
//		if ("CREATED".equals(type)) {
//			mdmCrecAccount.setMCreatedAt(date);
//			mdmCrecAccountHis.setMCreatedAt(date);
//			mdmCrecAccountDao.insertEntity(mdmCrecAccount);
//			mdmCrecAccountHisDao.insertEntity(mdmCrecAccountHis);
//		} else if ("UPDATE".equals(type)) {
//			mdmCrecAccount.setUpdateat("admin");
//			mdmCrecAccount.setMLastModifiedBy("admin");
//			mdmCrecAccount.setMLastModifiedAt(date);
//			mdmCrecAccountHis.setUpdateat("admin");
//			mdmCrecAccountHis.setMLastModifiedBy("admin");
//			mdmCrecAccountHis.setMLastModifiedAt(date);
//			mdmCrecAccountDao.updateEntity(mdmCrecAccount);
//			mdmCrecAccountHisDao.updateEntity(mdmCrecAccountHis);
//		} else {
//			if ("DELETED".equals(type) || "DISABLED".equals(type)) {
//				mdmCrecAccount.setMDataState(BigDecimal.ZERO);
//				mdmCrecAccountHis.setMDataState(BigDecimal.ZERO);
//				mdmCrecAccountDao.updateEntity(mdmCrecAccount);
//				mdmCrecAccountHisDao.updateEntity(mdmCrecAccountHis);
//			}
//			if ("ENABLED".equals(type)) {
//				mdmCrecAccount.setMDataState(BigDecimal.ONE);
//				mdmCrecAccountHis.setMDataState(BigDecimal.ONE);
//				mdmCrecAccountDao.updateEntity(mdmCrecAccount);
//				mdmCrecAccountHisDao.updateEntity(mdmCrecAccountHis);
//			}
//		}
	}
}
